package com.caiden_micheal.permission.mapper;

import com.caiden_micheal.permission.domain.Permission;
import com.caiden_micheal.permission.vo.PermissionQueryVO;
import com.caiden_micheal.permission.vo.PermissionTemplateSelectVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface PermissionMapper {
    List<Permission> findByIdListPaginationQuery(@Param("permission") PermissionQueryVO permissionQueryVO, @Param("authorityIdList") List<Long> authorityIdList);

    List<Permission> findPermissionListPaginationQueryExcludeUserId(@Param("permission") PermissionQueryVO permissionQueryVO, @Param("authorityIdList") List<Long> authorityIdList);

    Integer findPermissionListQueryCount(@Param("permission") PermissionQueryVO permissionQueryVO, @Param("authorityIdList") List<Long> authorityIdList);

    Integer findPermissionListQueryCountExcludeUserId(@Param("permission") PermissionQueryVO permissionQueryVO, @Param("authorityIdList") List<Long> authorityIdList);

    @Select("SELECT * FROM sys_authority WHERE authority_name=#{authorityName}")
    Permission findByName(@Param("authorityName") String authorityName);

    void addPermission(@Param("permission") Permission permission);

    @Select("SELECT * FROM sys_authority WHERE id=#{id}")
    Permission findById(@Param("id") Long id);

    void update(@Param("permission") Permission data);

    void updatePositionCountByIds(@Param("authorityList") List<Long> authorityIds, @Param("addFlag") Boolean addFlag);

    List<Permission> findBatchByIds(@Param("authorityIds") List<Long> authorityIds);

    void updateOrgCountByIds(@Param("authorityList") List<Long> authorityIds, @Param("addFlag") Boolean addFlag);

    void updateUserCountByIds(@Param("authorityList") List<Long> authorityIds, @Param("addFlag") Boolean addFlag);

    @Delete("DELETE FROM sys_authority WHERE id=#{permissionId}")
    void removeById(@Param("permissionId") Long permissionId);

    @Update("UPDATE sys_authority SET menu_id=#{menuId} WHERE id=#{id}")
    void updateMenuId(@Param("id") Long id, @Param("menuId") Long menuId);

    @Select("SELECT id FROM sys_authority WHERE menu_id=#{menuId}")
    List<Long> findIdByMenuId(Long menuId);

    @MapKey("id")
    Map<Long, Map<String, String>> findMapByIdList(@Param("permissionIdList") List<Long> permissionIdList);

    @Select("SELECT * FROM sys_authority WHERE menu_id=-1 AND authority_name LIKE CONCAT('%',#{displayName},'%') ORDER BY create_time DESC LIMIT 0,20")
    List<PermissionTemplateSelectVO> findPermissionSelectData(String displayName);
}